package orm

import (
	"playbook/ginsvr/src/global"

	"github.com/sirupsen/logrus"
)

type Subprocess struct {
	//子流程被使用后在subprocess表中存储的主键
	Subprocessid string `json:"id"`
	//被引用的子流程在process表中存储的主键
	Name                 string                 `json:"name"`
	Type                 int                    `json:"type"`
	Inputparammappingid  string                 `json:"-"`
	Inputparammapping    map[string]interface{} `gorm:"-" json:"input-parameter-mapping"`
	Outputparammappingid string                 `json:"-"`
	Outputparammapping   map[string]interface{} `gorm:"-" json:"output-parameter-mapping"`
	Transion             []Transion             `gorm:"-" json:"transition"`
}

type FullSubProcess struct {
	SubProcess Process `json:"subprocess"`
}

//子流程新增
func AddSubprocess(fullSubProcess FullSubProcess) interface{} {
	subprocess := fullSubProcess.SubProcess
	subprocess.Issubprocesss = true
	subprocess.Catagoryid = "0dc4276a65344255b0361fdaa4221337"
	return AddCommonProcess(subprocess)
}

//子流程更新
func UpdateSubprocess(fullSubProcess FullSubProcess) interface{} {
	subprocess := fullSubProcess.SubProcess
	subprocess.Issubprocesss = false
	return UpdateCommonProcess(subprocess)
}

//子流程查询
func GetSubProcess(name string) *FullSubProcess {
	var fullsubProcess FullSubProcess
	process := GetCommonProcess(name)
	fullsubProcess.SubProcess = *process
	return &fullsubProcess
}

//子流程删除
func DeleteSubProcess(subprocessname string) []string {
	//删除process表中的记录
	db.Exec("DELETE FROM process where name = $1", subprocessname)

	var deldir []string
	//1.删除subprocessjson - resource/facade/processname文件夹下的全部json
	srcjsondir := GetGeneSrcDefine("subprocessjson").Srcpath + global.PTHSEP + subprocessname
	deldir = append(deldir, srcjsondir)
	//2.删除subprocessbean - compose/processname文件夹下的全部参数类
	srcbeandir := GetGeneSrcDefine("subprocessbean").Srcpath + global.PTHSEP + subprocessname
	deldir = append(deldir, srcbeandir)
	//3.删除script - script/processname文件夹下该子流程的全部脚本信息
	srcscriptdir := GetGeneSrcDefine("script").Srcpath + global.PTHSEP + subprocessname
	deldir = append(deldir, srcscriptdir)
	return deldir
}

//被引用的子流程新增
func Addsubprocess(subprocess Subprocess, msgbms *[]MsgParamMapping, mapps *[]Mapping, transionList *[]Transion) {
	inputparammappingid := ""
	if subprocess.Inputparammapping["massage-bean-mapping"] != nil {
		inputparammap := subprocess.Inputparammapping["massage-bean-mapping"].(map[string]interface{})
		if inputparammap["mapping"] != nil {
			inputparammapping := getMappings(inputparammap["mapping"].([]interface{}))
			inputparammappingid = addMsgMapping(inputparammapping, msgbms, mapps)
		}
	}
	outputparammappingid := ""
	if subprocess.Outputparammapping["massage-bean-mapping"] != nil {
		outputparammap := subprocess.Outputparammapping["massage-bean-mapping"].(map[string]interface{})
		if outputparammap["mapping"] != nil {
			outputparammapping := getMappings(outputparammap["mapping"].([]interface{}))
			outputparammappingid = addMsgMapping(outputparammapping, msgbms, mapps)
		}

	}

	if subprocess.Transion != nil {
		addTransion(subprocess.Subprocessid, subprocess.Transion, "0", transionList)
	}

	err := db.Exec("INSERT INTO subprocess(subprocessid, name, inputparammappingid,  outputparammappingid) VALUES($1, $2, $3, $4)",
		subprocess.Subprocessid, subprocess.Name, inputparammappingid, outputparammappingid)
	if err.Error != nil {
		global.LOG.WithFields(logrus.Fields{
			"err":          err,
			"subprocessid": subprocess.Subprocessid,
		}).Error("add subprocess err.")
	}
}

//被引用的子流程查询
func Getsubprocess(actid string) Subprocess {
	var subprocess Subprocess
	db.Table("subprocess").Where("subprocessid = ?", actid).Find(&subprocess)

	//获取子流程输入参数映射
	subprocess.Inputparammapping = map[string]interface{}{"massage-bean-mapping": getMsgMapping(subprocess.Inputparammappingid)}

	//获取子流程输出参数映射
	subprocess.Outputparammapping = map[string]interface{}{"massage-bean-mapping": getMsgMapping(subprocess.Outputparammappingid)}

	//获取分支
	subprocess.Transion = getTransion(subprocess.Subprocessid, "0")

	return subprocess

}
